package net.cardgame.oraclecard.service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;

import net.cardgame.orcalecard.bean.DeckBean;
import net.cardgame.orcalecard.pref.SecurePreferences;
import net.cardgame.orcalecard.utils.ConstantValue;
import net.cardgame.orcalecard.utils.NetworkUtils;
import net.cardgame.orcalecard.utils.XmlParser;
import android.app.IntentService;
import android.content.Intent;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ServiceUpdateFileConfig extends IntentService {

	public ServiceUpdateFileConfig(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	public ServiceUpdateFileConfig() {
		super("ServiceUpdateFileConfig");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		ArrayList<DeckBean> listDeckBean = new ArrayList<DeckBean>();
		ArrayList<DeckBean> listDeckBeanSaved = new ArrayList<DeckBean>();
		String deckXml = "null";
		NetworkUtils networkUtils = new NetworkUtils();
		if (NetworkUtils.isNetworkConnected(this)) {
			deckXml = networkUtils.getStringFromUrl(ConstantValue.BASE_URL
					+ ConstantValue.LIST_DECK_SUB_URL);
		} else
			return;
		listDeckBean = new XmlParser().getDeckBeans(deckXml);
		// get sharedreference
		Type listOfDeckBean = new TypeToken<ArrayList<DeckBean>>() {
		}.getType();
		SecurePreferences appPreferences = new SecurePreferences(this,
				ConstantValue.APP_PREFERENCES,
				ConstantValue.PREFERENCES_SECRET_KEY, true);
		Gson gson = new Gson();
		String config = appPreferences
				.getString(ConstantValue.CONFIG_DECKBEAN_KEY);
		listDeckBeanSaved = gson.fromJson(config, listOfDeckBean);
		if (listDeckBean == null || listDeckBean.isEmpty()
				|| listDeckBeanSaved == null || listDeckBeanSaved.isEmpty())
			return;

		// test
		// DeckBean deck = listDeckBean.get(16);
		// DeckBean deck1 = new DeckBean();
		// deck1.deckId = 19;
		// deck1.deckName = "test";
		// deck1.isFree = true;
		// deck1.releaseDate = deck.releaseDate;
		// deck1.inAppPurchaseId = "12131121";
		// deck1.deckPrice = 132;
		// deck1.cardNumber = 44;
		// deck1.pathServerDownload = deck.pathServerDownload;
		// DeckBean deck2 = new DeckBean();
		// deck2.deckId = 20;
		// deck2.deckName = "test2";
		// deck2.isFree = true;
		// deck2.releaseDate = deck.releaseDate;
		// deck2.inAppPurchaseId = "12131121";
		// deck2.deckPrice = 132;
		// deck2.cardNumber = 44;
		// deck2.pathServerDownload = deck.pathServerDownload;
		// listDeckBean.add(deck1);
		// listDeckBean.add(deck2);
		// listDeckBean.remove(10);
		// listDeckBean.add(deck1);
		//
		// update list deck bean
		for (DeckBean deckBean : listDeckBean) {
			boolean add = true;
			for (DeckBean dec : listDeckBeanSaved) {
				if (dec.deckId == deckBean.deckId) {
					add = false;
					dec.deckName = deckBean.deckName;
					if (deckBean.isFree == false && dec.isFree == true) {
						dec.isUnlock = false;
					}
					dec.isFree = deckBean.isFree;
					dec.releaseDate = deckBean.releaseDate;
					dec.inAppPurchaseId = deckBean.inAppPurchaseId;
					dec.deckPrice = deckBean.deckPrice;
					dec.cardNumber = deckBean.cardNumber;
					dec.pathServerDownload = deckBean.pathServerDownload;
					break;
				}
			}
			if (add) {
				listDeckBeanSaved.add(deckBean);
			}
		}
		// remove deck bean if server has removed
		int size = listDeckBeanSaved.size();
		for (int i = 0; i < size; i++) {
			DeckBean deckBean = listDeckBeanSaved.get(i);
			boolean remove = true;
			for (DeckBean deckBean2 : listDeckBean) {
				if (deckBean.deckId == deckBean2.deckId) {
					remove = false;
					break;
				}
			}
			if (remove) {
				listDeckBeanSaved.remove(i);
				size--;
			}
		}
		// put shared reference
		String save = gson.toJson(listDeckBeanSaved, listOfDeckBean);
		appPreferences.put(ConstantValue.CONFIG_DECKBEAN_KEY, save);
	}
}
